package ans.pjz_offer_48;

import test.Entrance;

import java.util.HashSet;
import java.util.Set;

/**
 * #      剑指 Offer 48
 * 名称   最长不含重复字符的子字符串
 * 链接   https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof
 * 题解   [657](https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/solution)
 * 通过率 46.2%
 * 难道   中等
 */

public class Solution {
    @Entrance
    public int lengthOfLongestSubstring(String s) {
        int res = 0;
        Set<Character> set = new HashSet<>();
        for(int l = 0, r = 0; r < s.length(); r++) {
            char c = s.charAt(r);
            while(set.contains(c)) {
                set.remove(s.charAt(l++));
            }
            set.add(c);
            res = Math.max(res, r - l + 1);
        }

        return res;
    }
}